package com.Unidbg_hengdin.filehandle;

import com.Unidbg_hengdin.System_cm.StudySystemPropertyHook;
import com.alibaba.fastjson.util.IOUtils;
import com.github.unidbg.AndroidEmulator;
import com.github.unidbg.Emulator;
import com.github.unidbg.Module;
import com.github.unidbg.arm.backend.Unicorn2Factory;
import com.github.unidbg.file.FileResult;
import com.github.unidbg.file.IOResolver;
import com.github.unidbg.linux.android.AndroidEmulatorBuilder;
import com.github.unidbg.linux.android.AndroidResolver;
import com.github.unidbg.linux.android.SystemPropertyHook;
import com.github.unidbg.linux.android.SystemPropertyProvider;
import com.github.unidbg.linux.android.dvm.*;
import com.github.unidbg.linux.file.SimpleFileIO;
import com.github.unidbg.memory.Memory;

import java.io.File;

public class Study0903 extends AbstractJni  implements IOResolver {
    private final AndroidEmulator emulator;
    private final VM vm;
    private final Module module;
    private final Memory memory;

    private final DvmClass NativeHelperUtils;

    private final boolean logging;

    Study0903(boolean logging){
        this.logging =  logging;
        emulator = AndroidEmulatorBuilder.for64Bit()
                .setProcessName("com.xiaojianbang.app")
                .addBackendFactory(new Unicorn2Factory(true))
                // rootfs 虚拟文件系统
                .setRootDir(new File("unidbg-android\\src\\test\\java\\com\\Unidbg_hengdin\\filehandle\\rootfs"))
                .build();

        emulator.getSyscallHandler().addIOResolver(this);


        memory = emulator.getMemory();
        memory.setLibraryResolver(new AndroidResolver(23));

        vm = emulator.createDalvikVM();
        vm.setJni(this);
        vm.setVerbose(logging);

        DalvikModule dm = vm.loadLibrary(new File("unidbg-android/src/test/java/com/Unidbg_hengdin/filehandle/libxiaojianbang.so"),false);

        module = dm.getModule();

        NativeHelperUtils = vm.resolveClass("com/xiaojianbang/ndk/NativeHelper");

        // 验证 setRootDir 是否生效
        File rootDir = emulator.getFileSystem().getRootDir();
        System.out.println( "rootDir : " + rootDir);
        /***
         *
         *
         * 设置前：C:\Users\ADMINI~1\AppData\Local\Temp\rootfs\default
         * 设置后：unidbg-android\src\test\java\com\xiaojianbang\filehandle\rootfs
         */


    }

    void destory(){
        IOUtils.close(emulator);
        if(logging){
            System.out.println("destory ！");
        }
    }
    public static void main(String[] args){
        Study0903 test = new Study0903(true);
        test.callFun();
        test.destory();
    }

    void callFun(){
        StringObject encodeRetval = NativeHelperUtils.callStaticJniMethodObject(emulator, "readSomething()"); // 执行Jni方法
        System.out.println("fileRead res : " + encodeRetval.getValue());
    }

    @Override
    public FileResult resolve(Emulator emulator, String pathname, int oflags) {
        System.out.println("pathname -------------: " + pathname);
        if("/proc/self/maps".equals(pathname)){
            return FileResult.success(new SimpleFileIO(oflags,new File("F:\\unidbg-master\\unidbg-android\\src\\test\\java\\com\\Unidbg_hengdin\\filehandle\\maps"),pathname));
//            return FileResult.success(new ByteArrayFileIO(oflags, pathname,"hengdin test unidbg filehandler !".getBytes()));
        }
        return null;
    }


}
